package com.findyou.service.impl;

import com.findyou.mapper.UserMapper;
import com.findyou.pojo.Result;
import com.findyou.pojo.User;
import com.findyou.service.UserService;
import com.findyou.utils.JwtUtil;
import com.findyou.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Title: UserServiceImpl
 * @Author 贾金博
 * @Package com.findyou.service
 * @Date 2024/3/9 10:47
 * @description:
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean findByUsername(String username) {
        User user = userMapper.findByUsername(username);
        if(user == null) { // 表示未被占用
            return true;
        }
        return false;
    }

    @Override
    public Result register(String username, String password) {
        Date createTime = new Date();
        Date updateTime = new Date();
        password = Md5Util.getMD5String(password); // 进行密码的加密

        userMapper.register(username, password, createTime, updateTime); // 但是别人起的名字是 add

        return Result.success(null);
    }

    @Override
    public Result findByUsername2(String username, String password) {
        User user = userMapper.findByUsername(username);
        if(user == null) {
            return Result.error("用户不存在");
        }
        if(user == null || !password.equals(user.getPassword())) { // 登录失败
            return Result.error("密码错误");
        } else {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", user.getUsername());
            String token = JwtUtil.genToken(claims);
            //注册
            return Result.success(token);
        }
    }

    @Override
    public Result userInfo(int id) {
        User user = userMapper.getUserById(id);
        return Result.success(user);
    }

    @Override
    public Result update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
        System.out.println("id = " + user.getId());
        return Result.success("操作成功");
    }

    @Override
    public Result updateAvatar(String avatarUrl, int id) {
        userMapper.updateAvatar(avatarUrl, id);
        return Result.success("操作成功");
    }

    @Override
    public Result updatePwd(int id, String oldPwd, String newPwd, String rePwd) {
        //还要对密码进行加密
        User user = userMapper.getUserById(id);
        if(user.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
            if(!newPwd.equals(rePwd)) {
                return Result.error("两次输入的密码不一致");
            }
            //加密
            newPwd = Md5Util.getMD5String(newPwd);
            userMapper.updatePwd(newPwd, id);
            return Result.success("操作成功");
        } else {
            return Result.error("旧密码不正确");
        }
    }
}
